#include <game.h>

console::console() : base("console")
{
	text = new stringw(L"");
}

console::~console()
{
	
}

int console::init(coeur *c,IGUIEnvironment *e)
{
	base::init(0,c);
	env = e;
	
	cons = env->addImage(rect<s32>(0,0,600,420));
	tx  = env->addEditBox(L"", rect<s32>(0, 20, 600, 420),true,cons);
	box = env->addEditBox(L"", rect<s32>(0, 0, 600, 20),true,cons);
	log("Console env init");

	tx->setEnabled(false);
	tx->setDrawBorder(false);
	tx->setWordWrap(true);
	tx->setMultiLine(true);
	tx->setAutoScroll(true);
	tx->setTextAlignment(EGUIA_UPPERLEFT, EGUIA_LOWERRIGHT);
	log("Tx configured");

	cons->setVisible(false);
	return 1;
}

int console::update()
{
	if (cons->isVisible())
		env->setFocus(box);
	else
		env->setFocus(0);
	
	return 1;
}

int console::close()
{
	base::close();
	return 1;
}


bool console::OnEvent(const SEvent& event)
{
	if ((event.EventType==EET_GUI_EVENT) && (event.GUIEvent.EventType==EGET_EDITBOX_ENTER))
	{
		if (stringw(L"position")==box->getText())
		{
			*text = *text + L"Camera position :\n	" + mycore->cam->getPosition().X + L"\n	" + mycore->cam->getPosition().Y + L"\n	" + mycore->cam->getPosition().Z + L"\n";
			log("Display camera position");
		}
		else if (stringw(L"rotation")==box->getText())
		{
			*text = *text + L"Camera rotation :\n	" + mycore->cam->getRotation().X + L"\n	" + mycore->cam->getRotation().Y + L"\n	" + mycore->cam->getRotation().Z + L"\n";
			log("Display rotation position");
		}
		else if (stringw(L"target")==box->getText())
		{
			*text = *text + L"Target position :\n	" + mycore->cam->getTarget().X + L"\n	" + mycore->cam->getTarget().Y + L"\n	" + mycore->cam->getTarget().Z + L"\n";
			log("Display target position");
		}
		else if (stringw(L"exit")==box->getText())
		{
			*text = *text + L"Close console\n";
			cons->setVisible(false);
			log("Closing console");
		}
		else if (stringw(L"quit")==box->getText())
		{
			*text = *text + L"Quitting\n";
			mycore->q=true;
			log("Quitting by console");
		}
		else
		{
			*text = *text + L"Unrognized command : " + box->getText() + L"\n";
			log(stringc("Unrecognized command : ") + box->getText());
		}
		tx->setText(text->c_str());
		box->setText(L"");
		return true;
	}
	if (cons->isVisible() && (event.EventType==EET_KEY_INPUT_EVENT))
		return true;
	if ((event.EventType==EET_KEY_INPUT_EVENT)&&(event.KeyInput.PressedDown)&&(event.KeyInput.Key==KEY_KEY_C))
		cons->setVisible(true);
	return false;
}


